//
//  RetrieveSetPayPasswordViewController.m
//  UcarTest
//
//  Created by 陈曦 on 16/3/18.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "RetrieveSetPayPasswordViewController.h"
#import "Define.h"
#import "MainPageViewController.h"
#import "Userinfo.h"
#import "AFNetworking.h"
#import "NetRequestGroup.h"
#import "Access_token.h"
#import "MBProgressHUD.h"

#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"


@interface RetrieveSetPayPasswordViewController ()

@end

@implementation RetrieveSetPayPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"找回支付密码";
    self.navigationItem.titleView = titleLabel;
    //重定义返回键
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 22, 22);
    [back setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backBar;
    
    //定义视图背景色
    self.view.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1];
    
    //添加输入框视图
    [self inputTextField];
        
}


//==============================================================================

#pragma mark 输入框搭建
-(void)inputTextField{
    
    CGFloat inputSpaceValue = kScreenHeightEight * 0.7;
    
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(-0.5, kScreenHeightEight, kScreenWidth + 1, inputSpaceValue * 2)];
    inputView.layer.borderColor = [UIColor colorWithRed:215 / 255.0 green:215 / 255.0 blue:215 / 255.0 alpha:1].CGColor;
    inputView.layer.borderWidth = 0.5;
    inputView.layer.masksToBounds = YES;
    inputView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:inputView];
    
    //中间间隔线
    UILabel *septaLine = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kScreenHeightEight * 0.7, kScreenWidth - kScreenWidthTen / 2, 0.5)];
    septaLine.backgroundColor = lineColor;
    [inputView addSubview:septaLine];
    
    UIImageView *phoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, inputSpaceValue / 4, inputSpaceValue / 2 * 1.23 / 1.55, inputSpaceValue / 2)];
    phoneImageView.image = [UIImage imageNamed:@"iconfont-password"];
    [inputView addSubview:phoneImageView];
    
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, inputSpaceValue / 4 + inputSpaceValue, inputSpaceValue / 2 * 1.76 / 2.15, inputSpaceValue / 2)];
    passwordImageView.image = [UIImage imageNamed:@"iconfont-npw"];
    [inputView addSubview:passwordImageView];
    
    //新密码输入框框
    self.nNewPasswordsTextField = [[UITextField alloc] initWithFrame:CGRectMake(kScreenWidthTen * 1.5, inputSpaceValue / 4, kScreenWidth / 2, inputSpaceValue / 2)];
    self.nNewPasswordsTextField.delegate = self;
    self.nNewPasswordsTextField.placeholder = @"请输入新密码";
    self.nNewPasswordsTextField.secureTextEntry = YES;
    self.nNewPasswordsTextField.textAlignment = NSTextAlignmentLeft;
    [inputView addSubview:self.nNewPasswordsTextField];
    
    //密码确认输入框
    self.repeatPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(kScreenWidthTen * 1.5, inputSpaceValue / 4 + inputSpaceValue, kScreenWidth / 2, inputSpaceValue / 2)];
    self.repeatPasswordTextField.delegate = self;
    self.repeatPasswordTextField.placeholder = @"请再次输入新密码";
    self.repeatPasswordTextField.secureTextEntry = YES;//密文模式
    self.repeatPasswordTextField.textAlignment = NSTextAlignmentLeft;
    [inputView addSubview:self.repeatPasswordTextField];
    
    
    //确定按钮
    self.determineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.determineBtn.frame = CGRectMake(kScreenWidthTen / 2, kScreenHeightEight * 3, kScreenWidth - kScreenWidthTen, kScreenHeightEight / 2);
    self.determineBtn.backgroundColor = mainColor;
    self.determineBtn.layer.cornerRadius = 5;
    [self.determineBtn setTitle:@"确认" forState:UIControlStateNormal];
    [self.determineBtn addTarget:self action:@selector(determinePush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.determineBtn];
    
    
    //创建一个轻拍手势 回收键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    
    [self.view addGestureRecognizer:tap];
    
}

//==============================================================================
#pragma mark 确定后跳转
- (void)determinePush{
    
    if ([self.nNewPasswordsTextField.text isEqualToString:self.repeatPasswordTextField.text]) {
        //密码一致
        if ([[self.nNewPasswordsTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] < 6) {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能少于6位" preferredStyle:  UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        } else {
            
            if ([[self.nNewPasswordsTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 20) {
                //初始化提示框；
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码只能是6到20位" preferredStyle:  UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //点击按钮的响应事件；
                }]];
                //弹出提示框；
                [self presentViewController:alert animated:true completion:nil];
                
            } else {
                
                [self NetworkRequests];
            }
        }
        
    } else {
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不一致" preferredStyle:  UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    }
    
    
}

//==============================================================================
#pragma mark 网络请求
- (void) NetworkRequests{
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"mobile" : self.registerMobile,
                           @"password" : self.nNewPasswordsTextField.text,
                           @"code" : self.registerCode,
                           @"verify_password" : self.repeatPasswordTextField.text
                           };
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName,kNetRetrievePayPassword];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSDictionary *dic = responseObject;
        
        self.status = dic[@"status"];
        
        self.msg = dic[@"msg"];
        
        
        //返回值分析
        [self resultAnalysis];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
        
    }];
    
}
//==============================================================================
#pragma mark 返回值分析
- (void) resultAnalysis {
    
    int status = [self.status intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 3:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 4:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 5:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
            
        case 100:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                //回收键盘
                [self.view endEditing:YES];
                
                MainPageViewController *mainVC = [[MainPageViewController alloc] init];
                
                [self.navigationController pushViewController:mainVC animated:nil];
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    
}

//==============================================================================



//结束编辑,点击空白回收键盘
- (void)tap
{
    //回收键盘
    [self.view endEditing:YES];
}

//只能输入字母和数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

@end
